﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BasicCMS.Tests
{
    [TestClass]
    public class DependencyTest
    {
        private IContainer container;

        [TestInitialize]
        public void Initialize()
        {
            ContainerBuilder builder = new ContainerBuilder();
            var assemblies = Util.LoadAppAssemblies();
            builder.RegisterAssmelbies(assemblies);
            container=builder.Build();
        }

        [TestMethod]
        public void test_create_transit_instance()
        {
            var ta=container.Resolve<ITransitA>();
            var taa = container.Resolve<ITransitA>();
            var tb = container.Resolve<ITransitB>();
            Assert.IsNotNull(ta);
            Assert.IsNotNull(tb);
            Assert.AreNotEqual(taa, ta);
        }

        [TestMethod]
        public void test_create_transit_instance_from_config()
        {
            var tc = container.Resolve<ITransitC>();
            Assert.IsNotNull(tc);
            Assert.IsTrue(tc is TransitCC);
        }

        [TestMethod]
        public void test_create_single_instance()
        {
            var sa = container.Resolve<ISingleInstance>();
            var sa2 = container.Resolve<ISingleInstance>();
            Assert.IsNotNull(sa);
            Assert.IsNotNull(sa2);
            Assert.AreEqual(sa, sa2);
        }

        [TestMethod]
        public void test_create_single_instance_from_config()
        {
            var sa = container.Resolve<ISingleInstanceA>();
            Assert.IsNotNull(sa);
            Assert.IsTrue(sa is SingleInstanceA);
        }

        [TestMethod]
        [ExpectedException(typeof(Autofac.Core.Registration.ComponentNotRegisteredException))]
        public void test_create_no_registed()
        {
            var n = container.Resolve<INull>();
        }
    }

    #region transit
    public interface ITransitA
    {
    }

    public class TransitA : ITransitA
    {
    }

    public interface ITransitB
    {
    }

    [ComponentRegistry]
    public class TransitB : ITransitB
    {
    }

    public interface ITransitC
    {
    }

    [ComponentRegistry(Lifetime.Transient, "tran", "CC")]
    public class TransitCC : ITransitC
    {
    }

    [ComponentRegistry(Lifetime.Transient, "tran", "C1")]
    public class TransitCC1 : ITransitC
    {
    }


    #endregion

    #region single

    public interface ISingleInstance
    {
    }

    [ComponentRegistry(Lifetime.Singleton)]
    public class SingleInstance : ISingleInstance { }

    public interface ISingleInstanceA
    {
    }

    [ComponentRegistry(Lifetime.Singleton, "single", "a")]
    public class SingleInstanceA : ISingleInstanceA { }

    [ComponentRegistry(Lifetime.Singleton, "single", "b")]
    public class SingleInstanceB : ISingleInstanceA { }
    #endregion

    #region single

    public interface INull
    {
    }

    [ComponentRegistry(false)]
    public class INullImpl : INull { }

    #endregion
}
